package com.kgc.kmall.kmallcloudmanagerservice.controller;

import com.kgc.kmall.bean.PmsBaseCatalog1;
import com.kgc.kmall.bean.PmsBaseCatalog2;
import com.kgc.kmall.bean.PmsBaseCatalog3;
import com.kgc.kmall.service.CatalogService;
import com.kgc.kmall.util.ResultEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CataController {

    @Resource
    CatalogService catalogService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getCatalog1")
    @HystrixCommand(fallbackMethod = "cataListHystrix")
    public List<PmsBaseCatalog1> getCatalog1(){
        List<PmsBaseCatalog1> catalog1List = catalogService.getCatalog1();
        return catalog1List;
    }
    public List<PmsBaseCatalog1> cataListHystrix(){
        return null;
    }

    @RequestMapping("/getCatalog2/{catalog1Id}")
    public List<PmsBaseCatalog2> getCatalog2(@PathVariable Integer catalog1Id){
        List<PmsBaseCatalog2> catalog2 = catalogService.getCatalog2(catalog1Id);
        return catalog2;
    }

    @RequestMapping("/getCatalog3/{catalog2Id}")
    public List<PmsBaseCatalog3> getCatalog3(@PathVariable Integer catalog2Id){
        List<PmsBaseCatalog3> catalog3 = catalogService.getCatalog3(catalog2Id);
        return catalog3;
    }

    //注册进来的微服务~获取一些消息
    @GetMapping("/")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery--->"+services);
        List<ServiceInstance> instances = discoveryClient.getInstances("KMALLCLOUD-MANAGER-SERVICE");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost());
            System.out.println(instance.getScheme());
        }
        return this.discoveryClient;
    }

}
